package com.xiaozhen.common.core.exception;

import com.xiaozhen.common.core.domain.AjaxResult;
import com.xiaozhen.common.core.utils.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/9/3 15:30
 * @description:
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 业务异常
     */
    @ExceptionHandler(CustomException.class)
    public AjaxResult businessException(CustomException e) {
        if (StringUtils.isNull(e.getCode())) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.error(e.getCode(), e.getMessage());
    }

    /**
     * 非法请求异常
     */
    @ExceptionHandler(IllegalRequestException.class)
    public AjaxResult businessException(IllegalRequestException e) {
        if (StringUtils.isNull(e.getCode())) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.error(e.getCode(), e.getMessage());
    }
}
